package com.lisx.module_memo.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TimeUtils {

    public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF3 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF4 = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());

    public static final SimpleDateFormat DEFAULT_SDF5 = new SimpleDateFormat("yyyy", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF6 = new SimpleDateFormat("MM", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF7 = new SimpleDateFormat("dd", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF8 = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());

    public final static long minute = 60 * 1000;// 1分钟
    public final static long hour = 60 * minute;// 1小时
    public final static long day = 24 * hour;// 1天
    public final static long month = 31 * day;// 月
    public final static long year = 12 * month;// 年


    public static String getParseDayTextTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        return format.format(date);
    }

    public static String getParseDayTextTime2(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String parseYearTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF5);
    }

    public static String parseMonthTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF6);
    }

    public static String parseDayTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF7);
    }

    public static String parseTimeLong(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF);
    }

    public static String parseHMTimeLong(long utcTime) {

        return parseTimeLong(utcTime, DEFAULT_SDF3);
    }

    public static String parseDayTimeLong(long utcTime) {

        return parseTimeLong(utcTime, DEFAULT_SDF2);
    }

    public static String parseDayTextTimeLong(long utcTime) {

        return parseTimeLong(utcTime, DEFAULT_SDF8);
    }

    public static String parseTimeLong(long milliseconds, SimpleDateFormat format) {
        try {
            return format.format(new Date(milliseconds * 1000));

        } catch (Exception e) {
            return "";
        }
    }

    public static String parseTimeLong(String milliseconds, SimpleDateFormat format) {
        try {
            return format.format(new Date(Long.parseLong(milliseconds) * 1000));

        } catch (Exception e) {
            return "";
        }
    }


    public static String getHourMinSecondTime(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = second % 3600;
        if (second >= 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }

        if (second == 0)
            return "";

        if (second < 3600 && second >= 60)
            return d + "分" + s + "秒";

        if (second < 60 && second > 0)
            return s + "秒";

        return h + "时" + d + "分" + s + "秒";

    }

    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    public static String get(Date date) {
        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - date.getTime();
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }

    /**
     * 获取两个日期的周数差
     * @param startTime
     * @param endTime
     * @return
     */
    /*public static long getDifferWeek(Date startTime, Date endTime) {
        Date startFirstDayOfWeek = getFirstDayOfWeek(startTime); // 获取这天所在的周的第一天
        Date endFirstDayOfWeek = getFirstDayOfWeek(endTime);

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startFirstDayOfWeek);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endFirstDayOfWeek);

        long dayOffset = getTimeDistance(startFirstDayOfWeek, endFirstDayOfWeek);

        long weekOffset = dayOffset / 7;
        return weekOffset;
    }*/

    /**
     * 获得两个日期间距多少秒
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getDifferSecond(Date beginDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(beginDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, fromCalendar.getMinimum(Calendar.HOUR_OF_DAY));
        fromCalendar.set(Calendar.MINUTE, fromCalendar.getMinimum(Calendar.MINUTE));
        fromCalendar.set(Calendar.SECOND, fromCalendar.getMinimum(Calendar.SECOND));
        fromCalendar.set(Calendar.MILLISECOND, fromCalendar.getMinimum(Calendar.MILLISECOND));

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, fromCalendar.getMinimum(Calendar.HOUR_OF_DAY));
        toCalendar.set(Calendar.MINUTE, fromCalendar.getMinimum(Calendar.MINUTE));
        toCalendar.set(Calendar.SECOND, fromCalendar.getMinimum(Calendar.SECOND));
        toCalendar.set(Calendar.MILLISECOND, fromCalendar.getMinimum(Calendar.MILLISECOND));

        long secondDistance = (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / 1000;
        secondDistance = Math.abs(secondDistance);
        return secondDistance;
    }

    public static long getDifferMinute(Date beginDate, Date endDate) {
        return getDifferSecond(beginDate,endDate) / 60;
    }

    public static long getDifferHour(Date beginDate, Date endDate) {
        return getDifferSecond(beginDate,endDate) / (60 * 60);
    }

    public static long getDifferDay(Date beginDate, Date endDate) {
        return getDifferSecond(beginDate,endDate) / (60 * 60 * 24);
    }


    /**
     * 获取两个日期的月数差
     * @param fromDate
     * @param toDate
     * @return
     */
    public static long getDifferMonth(Date fromDate, Date toDate) {
        Calendar fromDateCal = Calendar.getInstance();
        Calendar toDateCal = Calendar.getInstance();
        fromDateCal.setTime(fromDate);
        toDateCal.setTime(toDate);

        int fromYear =  fromDateCal.get(Calendar.YEAR);
        int toYear = toDateCal.get((Calendar.YEAR));
        if (fromYear == toYear) {
            return Math.abs(fromDateCal.get(Calendar.MONTH) - toDateCal.get(Calendar.MONTH));
        } else {
            int fromMonth = 12 - (fromDateCal.get(Calendar.MONTH) + 1);
            int toMonth = toDateCal.get(Calendar.MONTH) + 1;
            return Math.abs(toYear - fromYear - 1) * 12 + fromMonth + toMonth;
        }
    }

    public static long dateDiffYear(Date fromDate, Date toDate) {
        return getDifferMonth(fromDate,toDate) / 12;// 计算差多少年
    }

    public static long dateDiffDay(long diffTime) {
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        return diffTime / nd;// 计算差多少天
    }

    public static long dateDiffHour(long diffTime) {

        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        return diffTime % nd / nh;// 计算差多少小时
    }

    public static long dateDiffMinute(long diffTime) {

        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        return diffTime % nd % nh / nm;// 计算差多少分钟
    }

    public static long dateDiffSecond(long diffTime) {

        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        return diffTime % nd % nh % nm / 1000;
    }
    /*public static long dateDiffHour(long diffTime) {


        return diffTime % day / hour;// 计算差多少小时
    }


    public static long dateDiffMinute(long diffTime) {
        return diffTime % day % hour / minute;// 计算差多少分钟
    }

    public static long dateDiffSecond(long diffTime) {

        return diffTime % day % hour % minute / 1000;
    }*/

    public static boolean isCloseEnough(long createAt, long preCreateAt) {
        return preCreateAt - createAt < 60;
    }

    public static String formatMusicTime(long duration) {
        String time = "";
        long minute = duration / 60000;
        long seconds = duration % 60000;
        long second = Math.round((int) seconds / 1000);
        if (minute < 10) {
            time += "0";
        }
        time += minute + ":";
        if (second < 10) {
            time += "0";
        }
        time += second;
        return time;
    }

    private static int getDayofWeek(String dateTime) {
        Calendar cal = Calendar.getInstance();
        if (dateTime.equals("")) {
            cal.setTime(new Date(System.currentTimeMillis()));
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date date;
            try {
                date = sdf.parse(dateTime);
            } catch (ParseException e) {
                date = null;
                e.printStackTrace();
            }
            if (date != null) {
                cal.setTime(new Date(date.getTime()));
            }
        }
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static String getWeek(long time){
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        date.setTime(time);
        calendar.setTime(date);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(calendar.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        if("1".equals(mWay)){
            mWay ="天";
        }else if("2".equals(mWay)){
            mWay ="一";
        }else if("3".equals(mWay)){
            mWay ="二";
        }else if("4".equals(mWay)){
            mWay ="三";
        }else if("5".equals(mWay)){
            mWay ="四";
        }else if("6".equals(mWay)){
            mWay ="五";
        }else if("7".equals(mWay)){
            mWay ="六";
        }
        return "星期"+mWay;
    }

    public static String millis2FitTimeSpan(long millis, int precision) {
        millis = (millis - System.currentTimeMillis()) ;
        if (millis < 0) {
            return "0秒";
        }
        if (precision <= 0) return null;
        precision = Math.min(precision, 5);
        String[] units = {"天", "时", "分", "秒","毫秒"};
        if (millis == 0) return 0 + units[precision - 1];
        StringBuilder sb = new StringBuilder();
        if (millis < 0) {
            sb.append("-");
            millis = -millis;
        }
        int[] unitLen = {86400000, 3600000, 60000, 1000, 1};
        for (int i = 0; i < precision; i++) {
            if (millis >= unitLen[i]) {
                long mode = millis / unitLen[i];
                millis -= mode * unitLen[i];
                sb.append(mode).append(units[i]);
            }
        }
        return sb.toString();
    }

    public static String millis2FitTimeSpan2(long millis, int precision) {
        millis = (millis - System.currentTimeMillis()) ;
        if (precision <= 0) return null;
        precision = Math.min(precision, 5);
        String[] units = {"天", "时", "分", "秒","毫秒"};
        if (millis == 0) return 0 + units[precision - 1];
        StringBuilder sb = new StringBuilder();
        if (millis < 0) {
            millis = -millis;
        }
        int[] unitLen = {86400000, 3600000, 60000, 1000, 1};
        for (int i = 0; i < precision; i++) {
            if (millis >= unitLen[i]) {
                long mode = millis / unitLen[i];
                millis -= mode * unitLen[i];
                sb.append(mode).append(units[i]);
            }
        }
        return sb.toString();
    }

    public static String getDate(long time) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(new Date(time));
        int month = cal1.get(Calendar.MONTH);
        int year = cal1.get(Calendar.YEAR);
        int day = cal1.get(Calendar.DAY_OF_MONTH);
        String week = getWeek(cal1.get(Calendar.DAY_OF_WEEK));
        return year + "-" +  (month + 1) + "-" + day + "  " + week;
    }

    private static String getWeek(int dayOfWeek) {
        String week = "";
        if (dayOfWeek == 1) {
            week = "(星期日)";
        } else if (dayOfWeek == 2) {
            week = "(星期一)";
        } else if (dayOfWeek == 3) {
            week = "(星期二)";
        } else if (dayOfWeek == 4) {
            week = "(星期三)";
        } else if (dayOfWeek == 5) {
            week = "(星期四)";
        } else if (dayOfWeek == 6) {
            week = "(星期五)";
        } else if (dayOfWeek == 7) {
            week = "(星期六)";
        }
        return week;
    }

    public static boolean isSameDay(long date1,long date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(new Date(date1));
        int day = cal1.get(Calendar.DAY_OF_MONTH);
        int month = cal1.get(Calendar.MONTH);
        int year = cal1.get(Calendar.YEAR);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(new Date(date2));
        int day2 = cal2.get(Calendar.DAY_OF_MONTH);
        int month2 = cal2.get(Calendar.MONTH);
        int year2 = cal2.get(Calendar.YEAR);

        if (year == year2 && month == month2 && day == day2) {
            return true;
        }
        return false;
    }
}
